﻿using AnesSystem.Models.Model;
using AnesSystem.Services;
using AnesSystem.ViewModels.LaborAnalgesia;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    public class LaborEventUpdateViewModel:BaseViewModel<LaborEventUpdateViewModel>
    {
        #region Variables
        private PatientLaborEventModel _patientLaborEvent;
        private LaborEventAndMedicineCtrlViewModel _model;
        private LaborStatusItemViewModel _statusModel;



        #endregion


        #region Constructor

        #endregion


        #region Properties
        public PatientLaborEventModel PatientLaborEvent
        {
            get
            {
                return _patientLaborEvent;
            }

            set
            {
                _patientLaborEvent = value;
            }
        }

        public LaborEventAndMedicineCtrlViewModel Model
        {
            get
            {
                return _model;
            }

            set
            {
                _model = value;
            }
        }

        public LaborStatusItemViewModel StatusModel
        {
            get
            {
                return _statusModel;
            }

            set
            {
                _statusModel = value;
            }
        }

        public string Save()
        {

            PatientLaborEvent.BeginTime = PatientLaborEvent.BeginTime.AddSeconds(-PatientLaborEvent.BeginTime.Second).AddMilliseconds(-PatientLaborEvent.BeginTime.Millisecond);
            if (string.IsNullOrEmpty(PatientLaborEvent.Name))
            {
                MessageBoxCommon.ShowDialog("事件名称不能为空!", MessageBoxCommon.OK);
                return Constants.FAILED;
            }
            if (PatientLaborEvent.BeginTime < PatientLink.Status.InLaborTime && !("分娩镇痛开始").Equals(PatientLaborEvent.Name))
            {
                MessageBoxCommon.ShowDialog("开始时间应该大于镇痛开始时间!", MessageBoxCommon.OK);
                return Constants.FAILED;
            }
            Save2DB();
            RefreshWin();
            return Constants.SUCCESS;
        }

        public  void DelEvent()
        {
            if (("分娩镇痛开始").Equals(PatientLaborEvent.Name))
            {
                MessageBoxCommon.ShowDialog("开始事件不能删除!", MessageBoxCommon.OK);
                return;
            }
            PatientLaborEventService.Instance.DeleteById(PatientLaborEvent.Id);
            RefreshWin();
        }

        private void RefreshWin()
        {
            if (("分娩镇痛开始").Equals(PatientLaborEvent.Name))
            {
                PatientLink.Status.InLaborTime = PatientLaborEvent.BeginTime;
                DateTime date = PatientLaborEvent.BeginTime;
                date = date.AddMinutes(-date.Minute % 5);
                DataManager.Instance.CurrentPatientLink.Status.LaborRecordStartTime = date;
                PatientLinkService.Instance.AddUpdate(PatientLink);
                LaborAnalgesiaProcessViewModel.Instance.RefreshTimeData();
            }
            Model.Refresh();
            StatusModel.Refresh();
        }

        private void Save2DB()
        {
            PatientLaborEventService.Instance.AddUpdate(PatientLaborEvent);
        }

        


        #endregion


        #region 依赖属性


        #endregion


        #region Event


        #endregion


        #region PrivateMethods


        #endregion


        #region Public/ProtectedMethods


        #endregion


        #region EventHandlers


        #endregion
    }
}
